'use strict';

const path = require('path');
const {BrowserWindow, Notification} = require('electron');
const {getMainWindow} = require('ee-core/electron');
const {isDev, isProd, getBaseDir} = require('ee-core/ps');
const {getConfig} = require('ee-core/config');
const {isFileProtocol} = require('ee-core/utils');
const {logger} = require('ee-core/log');
const CoreWindow = require("ee-core/electron/window");

/**
 * Window
 * @class
 */
class WindowService {

    constructor() {
        this.myNotification = null;
        this.windows = {}
    }

    /**
     * 创建新窗口
     */
    createWindow(args) {
        const {type, content, windowName, windowTitle} = args;
        let contentUrl = null;
        if (type == 'html') {
            contentUrl = path.join('file://', getBaseDir(), content)
        } else if (type == 'web') {
            contentUrl = content;
        } else if (type == 'vue') {
            let addr = 'http://localhost:8080'
            if (isProd()) {
                const {mainServer} = getConfig();
                if (isFileProtocol(mainServer.protocol)) {
                    addr = mainServer.protocol + path.join(getBaseDir(), mainServer.indexPath);
                } else {
                    addr = mainServer.protocol + mainServer.host + ':' + mainServer.port;
                }
            }

            contentUrl = addr + content;
        } else {
            // some
        }

        logger.info('[createWindow] url: ', contentUrl);
        const opt = {
            title: windowTitle,
            x: 10,
            y: 10,
            width: 980,
            height: 650,
            webPreferences: {
                contextIsolation: false,
                nodeIntegration: true,
            },
        }
        const win = new BrowserWindow(opt);
        const winContentsId = win.webContents.id;
        win.loadURL(contentUrl);
        if (isDev()) {
            win.webContents.openDevTools();
        }

        this.windows[windowName] = win;

        return winContentsId;
    }

    /**
     * 获取窗口内容id
     */
    getWCid(args) {
        const {windowName} = args;
        let win;
        if (windowName == 'main') {
            win = getMainWindow();
        } else {
            win = this.windows[windowName];
        }

        return win.webContents.id;
    }

    /**
     * 通过主进程的传输实现两个窗口之间的通信
     */
    communicate(args) {
        const {receiver, content} = args;
        if (receiver == 'main') {
            const win = getMainWindow();
            win.webContents.send('controller/os/window2ToWindow1', content);
        } else if (receiver == 'window2') {
            const win = this.windows[receiver];
            win.webContents.send('controller/os/window1ToWindow2', content);
        }
    }

    /**
     * 创建通知
     */
    createNotification(options, event) {
        const channel = 'controller/os/sendNotification';
        this.myNotification = new Notification(options);

        if (options.clickEvent) {
            this.myNotification.on('click', (e) => {
                let data = {
                    type: 'click',
                    msg: '您点击了通知消息'
                }
                event.reply(`${channel}`, data)
            });
        }

        if (options.closeEvent) {
            this.myNotification.on('close', (e) => {
                let data = {
                    type: 'close',
                    msg: '您关闭了通知消息'
                }
                event.reply(`${channel}`, data)
            });
        }

        this.myNotification.show();
    }

    /**
     * 关闭窗口
     */
    closeWindow(args) {
        const win = getMainWindow();
        win.close();
    }

    /**
     * 最小化窗口
     */
    minimizeWindow(args) {
        const win = getMainWindow();
        win.minimize()

    }

    /**
     * 最大化窗口
     */
    maximizeWindow(args) {
        const win = getMainWindow();
        win.maximize()
    }

    /**
     * 还原窗口
     */
    restoreWindow(args) {
        const win = getMainWindow();
        win.unmaximize()
    }

    setPositionWindow(args) {
        getMainWindow().setPosition(args.x, args.y, true);
    }

    getPositionWindow(args) {
        const position = getMainWindow().getPosition();
        logger.info('[getPositionWindow] position: ', position);
        return position;
    }

    getConfig(args) {
        const config = getConfig();
        return config ;
    }

}

WindowService.toString = () => '[class WindowService]';

module.exports = {
    WindowService,
    windowService: new WindowService()
};  